<?php
/**
 * Created by PhpStorm.
 * User: adophper <hello@adophper.com>
 * Date: 2017/1/8
 * Time: 9:25
 */
use yii\helpers\Html;
use common\components\TGridView;
use yii\helpers\Url;

?>
<div class="frontend-menu_list">
<?php
$gridColumns = [
    [
        'name' => 'selectedIds',
        'class' => 'kartik\grid\CheckboxColumn',
        'checkboxOptions' => function($model, $key, $index, $column) {
            return [
                'value' => $model->id,
            ];
        }
    ],
    [
        'class' => 'kartik\grid\SerialColumn',
        'header' => Yii::t('common','serial_number'),
    ],
    [
        'attribute'=>'name',
        'format'=>'text',
    ],
    'code',
    'url',
    'params',
    [
        'header'=> Yii::t('common', 'status'),
        'value' => function($model){
            return \common\helpers\TStringHelper::getStatusText($model->status);
        }
    ],
    [
        'header'=> Yii::t('common', 'is_single'),
        'value' => function($model){
            return \common\helpers\TStringHelper::getYesOrNo($model->is_single);
        }
    ],
    [
        'header'=> Yii::t('common', 'order'),
        'value' => function($model){
            return Html::textInput('orders['.$model->id.']', $model->order, ['class' => 'form-control', 'style' => 'width: 50px;']);
        },
        'format'=>'raw',
    ],
    [
        'class' => 'kartik\grid\ActionColumn',
        'header' => Yii::t('common', 'operation_button'),
        'template' => '{viewpop}{updatepop}{statusButton}{deleteButton}',
        'width' => '150px',
        'buttons' => [
            'viewpop' => function ($url, $model, $key) {
                return Html::a('<span class="glyphicon glyphicon-eye-open"></span>', '#', ['class'=>'ViewButton', 'title'=>Yii::t('common', 'view_button'), 'onclick' => 'modalLoad("formModal", "' . Yii::$app->urlManager->createUrl([Yii::$app->controller->id.'/view', 'id' => $key]) . '" , "' . Yii::t('common', 'view_button') . '");']);
            },
            'updatepop' => function ($url, $model, $key) {
                return Html::a('<span class="glyphicon glyphicon-pencil"></span>', '#', ['class' => 'EditButton', 'title' => Yii::t('common', 'edit_button'), 'onclick' => 'modalLoad("formModal", "' . Yii::$app->urlManager->createUrl([Yii::$app->controller->id.'/update', 'id' => $key]) . '" , "' . Yii::t('common', 'edit_button') . '");']);

            },
            'statusButton' => function ($url, $model, $key) {
                if ($model->status == \common\base\BaseActiveRecord::STATUS_FLAG_NORMAL) {
                    $title = Yii::t('common', 'change_status_stop');
                    $class = 'glyphicon glyphicon-pause';
                    $status = \common\base\BaseActiveRecord::STATUS_FLAG_STOP;
                }
                else
                {
                    $title = Yii::t('common', 'change_status_start');
                    $class = 'glyphicon glyphicon-play';
                    $status = \common\base\BaseActiveRecord::STATUS_FLAG_NORMAL;
                }
                return Html::a('<span class="'.$class.'"></span>', '#', ['id'=>'StatusButton', 'title'=> $title, 'onclick' => 'statusButton("' . Yii::$app->urlManager->createUrl([Yii::$app->controller->id.'/status', 'id' => $key, 'status' => $status]) . '");']);
            },
            'deleteButton' => function ($url, $model, $key) {
                return Html::a('<span class="glyphicon glyphicon-trash"></span>', '#', ['class' => 'DeleteButton', 'title' => Yii::t('common', 'delete_button'), 'onclick' => 'deleteButton("'.$key.'","'. Yii::$app->urlManager->createUrl([Yii::$app->controller->id.'/delete','id'=>$key]).'");']);

            },
        ],
    ],
];

$contentName = Yii::t('common', 'frontend_menus');

//if (empty($model)) {
    $addbtn = Html::button('<i class="glyphicon glyphicon-plus"></i> ' . Yii::t('common', 'add_button'), [
        'title' => Yii::t('common', 'add_button'), 'class' => 'btn btn-default greenBtn', 'onclick' => 'modalLoad("formModal", "' . Url::toRoute(['create', 'parent_id' => $treeNodeId]) . '" , "' . Yii::t('common', 'add_button') . '");'
    ]);
/*}else{
    $addbtn = '';
}*/


echo TGridView::widget([
    'id'=>'grid',
    'dataProvider' => $dataProvider,
    //'filterModel' => $searchModel,
    'columns' => $gridColumns,
    'panel' => [
        'type' => TGridView::TYPE_DEFAULT,
        'heading' => '<h3 class="panel-title" style="text-align: left;"><i class="glyphicon glyphicon-book"></i> ' .Yii::t('common', '{value}_record', ['value'=>$contentName]).'</h3>',
    ],
    'toolbar' => [
        ['content'=>
            $addbtn
            .' '.
            (\common\helpers\TStringHelper::canBeModified() ? Html::button('<i class="glyphicon glyphicon-refresh"></i> '.Yii::t('common', 'batch_update_button'),['title'=>Yii::t('common', 'batch_update_button'), 'class'=>'btn btn-default blueBtn', 'onclick' => 'batchUpdateOrderBtn("'.Yii::$app->urlManager->createUrl(['frontend-menu/batch-update-order']).'", "list-form")']) : '')
            //Html::button('<i class="glyphicon glyphicon-minus"></i> '.Yii::t('common', 'batch_delete_button'),['title'=>Yii::t('common', 'batch_delete_button'), 'class'=>'btn btn-default redBtn', 'onclick'=>'batchDeleteButton("'. Yii::$app->urlManager->createUrl('video-category/batch-delete').'");'])
        ],
//            '{export}',
//            '{toggleData}'
    ],
    'pjax'=>true,
    'pjaxSettings'=>[
        'neverTimeout'=>true,
    ]
]);
?>
</div>

<div class="text-right">
    <?php
    echo \yii\widgets\LinkPager::widget([
        'id' => 'menu_page',
        'pagination'=>$dataProvider->pagination,
    ]);
    ?>
</div>

<script>
    $(".pagination a").on('click', function(e){
        e.preventDefault();
        $.get($(this).attr('href'), {}, function(reponse){
            $("#list").html(reponse)
        })
    })
</script>